package com.dq.demos.web.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class YFKFormatDataUtil {

    /**
     * 将浙里办发票信息转化为费控接收数据格式
     * @param json
     * @return
     */
    public static JSONObject handlerYFKFormatData(JSONObject json,String temppath) throws Exception{
        // 发票类型：1:增值税电子普通发票;2:增值税电子专用发票;3:增值税普通发票;4:增值税专用发票;5:机动车销售统一发票;6:货物运输业增值税专用发票;
        // 8:增值税电子普通发票(成品油)；9:成品油普通发票(卷式);10:增值税普通发票(成品油);11:增值税专用发票(成品油);12:增值税普通发票(卷式)
        // 14:通行费增值税电子普通发票
        JSONObject fsJson = new JSONObject();
        String xbrlId = json.getString("xbrlId");
        YFKFormatDataUtil util = new YFKFormatDataUtil();
        switch (xbrlId) {
            case "zjinv_odn_r_issuer":  // 增值税普通发票(卷式)
            case "zjinv_usmv_issuer" : // 机动车销售统一发票
            case "zjinv_ucsu_issuer" : // 二手车销售统一发票
            case "zjginv_pmso_issuer" : // 私立医疗服务（门诊）电子发票
            case "zjginv_gms_issuer" : // 机动车销售统一发票
            case "zjginv_gmsh_issuer" : //  私立医疗服务（住院）电子发票
            case "zjginv_soggmp_issuer" : // 货物销售通用机打发票
            case "zjginv_rargmp_issuer" : // 修理修配通用机打发票
            case "zjginv_aougmp_issuer" : // 收购统一通用机打发票
            case "zjginv_rosgmp_issuer" : // 成品油销售通用机打发票
            case "zjginv_ebgmp_issuer" : // 出口业务通用机打发票
            case "zjginv_taxgmp_issuer" : // 税务机关代开通用机打发票
            case "zjginv_acqgmp_issuer" : // 农产品收购通用机打发票
            case "zjginv_hydgmp_issuer" : // 水电业通用机打发票
            case "zjginv_gragmp_issuer" : //  粮食收购通用机打发票
            case "inv_ord_issuer": // 增值税电子普通发票 1
                fsJson = util.inv_ord_issuerXML(json, "1",temppath); // 增值税电子普通发票
                break;
            case "zjinv_odn_issuer": // 增值税普通发票 1
                fsJson = util.zjinv_odn_issuerXML(json, "3",temppath); // 增值税普通发票
                break;
            case "inv_spcl_issuer": // 增值税电子专用发票 2
                fsJson = util.inv_spcl_issuerXML(json, "2",temppath); // 增值税电子专用发票
                break;
            case "zjinv_spe_issuer": // 增值税专用发票 2
                fsJson = util.zjinv_spe_issuerXML(json, "4",temppath); // 增值税专用发票
                break;
            case "inv_tlf_issuer": // 收费公路通行费增值税电子普通发票 1
                fsJson = util.inv_tlf_issuerXML(json, "14",temppath); // 收费公路通行费增值税电子普通发票
                break;
            case "zjginv_gen_issuer": // 通用（电子）发票 2 不确定类型 属于增值税电子发票
                fsJson = util.zjginv_gen_issuerXML(json, "3",temppath); // 通用（电子）发票
                break;
            case "zjinv_feord_issuer": // (全电)电子发票(普通发票) 1
                fsJson = util.zjinv_feord_issuerXML(json, "32",temppath); // (全电)电子发票(普通发票)
                break;
            case "zjinv_fespcl_issuer":  // (全电)电子发票(增值税专用发票)
                fsJson = util.zjinv_fespcl_issuerXML(json, "31",temppath);// (全电)电子发票(增值税专用发票)
                break;
            default: // 默认是 增值税电子普通发票
                fsJson = util.otherXml(json, "1",temppath);
        }
        JSONObject odfjson = new JSONObject();
        String fileBase64 = util.fileToBase64(temppath);
        odfjson.put("content", fileBase64); // BASE64
        odfjson.put("version","V2");
        int i = temppath.lastIndexOf("\\");
        String fileName = temppath.substring(i + 1);
        odfjson.put("fileName",fileName);
        JSONObject res = new JSONObject();
        res.put("data", fsJson);
        res.put("odf", odfjson);
        return res;
    }

    /**
     * 将文件转化为Base64
     * @param filePath 文件路径
     * @return
     */
    private  String fileToBase64(String filePath) {
        File file = new File(filePath);
        try (InputStream inputStream = new FileInputStream(file)) {
            // 获取文件大小
            long fileSize = file.length();
            // 创建一个相应大小的字节数组
            byte[] fileBytes = new byte[(int) fileSize];
            // 读取文件内容到字节数组
            inputStream.read(fileBytes);
            // 进行Base64编码
            return Base64.getEncoder().encodeToString(fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 增值税电子专用发票
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject inv_spcl_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", json.getString("NumberOfInvoice"));
        data.put("fpDm", json.getString("CodeOfInvoice"));
        data.put("invoiceNum", invoiceNum); // 发票号码
        data.put("invoiceCode", invoiceCode); // 发票代码
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject(); // 发票不走查验功能，因为是从发票平台下载的
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        data.put("jqbh",json.getString("NumberOfInvoiceMachine")); // 机器编号
        data.put("ssdq", json.getString("LocationOfInvoice")); // 发票所属地区
        data.put("jym", json.getString("IdentifyingCode")); // 校验码
        data.put("gmfDzdh",json.getString("AddressPhoneNumberOfPurchaser"));
        data.put("gmfYhzh",json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("xsfYhzh",json.getString("DepositBankAndAccountNumberOfSeller"));
        data.put("xsfDzdh",json.getString("AddressPhoneNumberOfSeller"));
        data.put("hjse", json.getString("TotalTaxAmount")); //
        data.put("bz", json.getString("Remarks")); //
        data.put("skr", json.getString("Payee")); //
        data.put("fhr", json.getString("Reviewer")); //
        data.put("kpr",json.getString("Issuer"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        data.put("filepath", temppath);
        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("xmmc",arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("ggxh",arr.getString("Specification"));
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("xmsl",arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("xmdj",arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje",arr.getBigDecimal("AmountExcludingTax"));
                item.put("se",arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100);
                items.add(item);
            }
        }

        return fsJson;

    }

    /**
     * 增值税普通发票
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject zjinv_odn_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("fpDm", invoiceCode); //
        data.put("fpHm", invoiceNum); //
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        //
        data.put("jqbh",json.getString("NumberOfInvoiceMachine")); // 机器编号
        data.put("ssdq", json.getString("LocationOfInvoice")); // 发票所属地区
        data.put("jym", json.getString("IdentifyingCode")); // 校验码
        data.put("gmfDzdh",json.getString("AddressPhoneNumberOfPurchaser"));
        data.put("gmfYhzh",json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("xsfYhzh",json.getString("DepositBankAndAccountNumberOfSeller"));
        data.put("xsfDzdh",json.getString("AddressPhoneNumberOfSeller"));
        data.put("hjse", json.getString("TotalTaxAmount")); //
        data.put("bz", json.getString("Remarks")); //
        data.put("skr", json.getString("Payee")); //
        data.put("fhr", json.getString("Reviewer")); //
        data.put("kpr",json.getString("Issuer"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        data.put("zfbz",json.getString("VoidMark") != null ? json.getString("VoidMark").replace("-", "") : ""); // N-未作废Y-已作废
        data.put("filepath", temppath);
        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
//                "items":[{"dw":"盒","ggxh":"0.5mg×10片×10板","se":356.41, "sl":13,"xmdj":13.70796460,"xmje":2741.59,"xmmc":"化学药品制剂*甲钴胺片(弥可保)/卫材(中国)药业", "xmsl":200
                item.put("xmmc", arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("ggxh",arr.getString("Specification"));
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("xmsl",arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("xmdj",arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje",arr.getBigDecimal("AmountExcludingTax"));
                item.put("se",arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", ""))/ 100 );
                items.add(item);
            }
        }
        return fsJson;
    }

    /**
     * 增值税电子普通发票
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject inv_ord_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        JSONArray items = new JSONArray();
        fsJson.put("data", data);
        data.put("items", items);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("xsfDzdh",json.getString("AddressPhoneNumberOfSeller")); // 销方地址电话
        data.put("fpHm", json.getString("NumberOfInvoice"));
        data.put("fpDm", json.getString("CodeOfInvoice"));
        data.put("invoiceNum", invoiceNum); // 发票号码
        data.put("invoiceCode", invoiceCode); // 发票代码
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jym", json.get("IdentifyingCode"));
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("gmfYhzh", json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("skr", json.getString("Payee"));
        data.put("kpr", json.getString("Issuer"));
        data.put("filepath", temppath);
        data.put("xsfYhzh", json.getString("AddressPhoneNumberOfSeller"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("ggxh",arr.get("Specification"));
                item.put("se", arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100);
                item.put("xmdj", arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje", arr.getBigDecimal("AmountExcludingTax"));
                item.put("xmmc", arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("xmsl", arr.getBigDecimal("QuantityOfGoodsOrServices"));
                items.add(item);
            }
        }
        return fsJson;
    }

    /**
     * 增值税专用发票
     * @param json
     * @return
     * @throws Exception
     */
    public JSONObject zjinv_spe_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", invoiceNum);
        data.put("fpDm", invoiceCode);
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        //
        data.put("jym", json.get("IdentifyingCode"));
        data.put("gmfYhzh", json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("skr", json.getString("Payee"));
        data.put("kpr", json.getString("Issuer"));
        data.put("filepath", temppath);
        data.put("xsfYhzh", json.getString("AddressPhoneNumberOfSeller"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        data.put("zfbz",json.getString("VoidMark") != null ? json.getString("VoidMark").replace("-", "") : "");
        data.put("zfrq",json.getString("DateOfVoid"));

        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("xmmc",arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("ggxh", arr.getString("Specification"));
                item.put("dw", arr.getString("UnitOfGoodsOrServices"));
                item.put("xmsl",arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("xmdj",arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje", arr.getBigDecimal("AmountExcludingTax"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) /100);
                item.put("TaxAmount", arr.getBigDecimal("TaxAmount"));
                items.add(item);
            }
        }
        return fsJson;
    }

    /**
     * 通用（电子）发票
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject zjginv_gen_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", invoiceCode);
        data.put("fpDm", invoiceCode);
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        //
        data.put("jym", json.get("IdentifyingCode"));
        data.put("gmfYhzh", json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("skr", json.getString("Payee"));
        data.put("kpr", json.getString("Issuer"));
        data.put("filepath", temppath);
        data.put("xsfYhzh", json.getString("AddressPhoneNumberOfSeller"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        data.put("zfbz",json.getString("VoidMark") != null ? json.getString("VoidMark").replace("-", "") : "");
        data.put("zfrq",json.getString("DateOfVoid"));
        data.put("checkCode", json.getString("VerificationCode"));
        data.put("ssdq",json.getString("LocationOfInvoice")); // 所属地区

        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("xmmc", arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("xmsl",arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("ggxh", arr.getString("Specification"));
                item.put("dw", arr.getString("UnitOfGoodsOrServices"));
                item.put("xmdj", arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100 );
                item.put("se",arr.getBigDecimal("TaxAmount"));
                item.put("xmje",arr.getBigDecimal("AmountExcludingTax"));
                items.add(item);
            }
        }
        return fsJson;

    }

    /**
     * 收费公路通行费增值税电子普通发票
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject inv_tlf_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        JSONArray items = new JSONArray();
        fsJson.put("data", data);
        data.put("items", items);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", json.getString("NumberOfInvoice"));
        data.put("fpDm", json.getString("CodeOfInvoice"));
        data.put("invoiceNum", invoiceNum); // 发票号码
        data.put("invoiceCode", invoiceCode); // 发票代码
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        data.put("filepath", temppath);
        data.put("bz", json.getString("Remarks"));
        data.put("skr",json.getString("Payee"));
        data.put("fhr",json.getString("Reviewer"));
        data.put("kpr",json.getString("Issuer"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        data.put("gmfDzdh",json.getString("AddressPhoneNumberOfPurchaser"));
        data.put("gmfYhzh",json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("xsfYhzh",json.getString("DepositBankAndAccountNumberOfSeller"));
        data.put("xsfDzdh",json.getString("AddressPhoneNumberOfSeller"));
        data.put("hjse",json.getString("TotalTaxAmount"));
        data.put("jym",json.getString("IdentifyingCode"));
        data.put("jqbh",json.getString("NumberOfInvoiceMachine")); // 机器编号
        data.put("ssdq",json.getString("LocationOfInvoice")); // 所属地区
        data.put("mmq",json.getString("SecurityCode")); // 密码区
        Object object = json.get("InformationOfTollFeeDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTollFeeDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("txfCph", arr.getString("PlateNumber"));
                item.put("txfLx", arr.getString("TypesOfVehicle"));
                item.put("txfTxrqq", arr.getString("EntranceDate"));
                item.put("txfTxrqz", arr.getString("ExitDate"));
                item.put("xmmc", arr.getString("NameOfTollFeeItems"));
                item.put("ggxh",arr.get("Specification"));
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("xmsl", arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("xmdj", arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje", arr.getBigDecimal("AmountExcludingTax"));
                item.put("se", arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", ""))/100);
                items.add(item);
            }
        }
        return fsJson;

    }

    /**
     * (全电)电子发票(普通发票)
     *
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject zjinv_feord_issuerXML(JSONObject json,String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", json.getString("NumberOfInvoice"));
        data.put("fpDm", invoiceCode);// 不存在发票号码
        data.put("invoiceNum", invoiceNum); // 发票号码
        data.put("invoiceCode", invoiceCode); // 发票代码
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("bz", json.getString("Remarks"));
        data.put("kpr",json.getString("Issuer"));
        data.put("filePath", temppath);
        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("ggxh",arr.getString("Specification"));
                item.put("se",arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100 );
                item.put("xmdj",arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje", arr.getBigDecimal("AmountExcludingTax"));
                item.put("xmmc", arr.get("NameOfTaxableGoodsOrServices"));
                item.put("xmsl", arr.getBigDecimal("QuantityOfGoodsOrServices"));
                items.add(item);
            }
        }
        return fsJson;
    }

    /**
     * (全电)电子发票(增值税专用发票)
     * @param json
     * @return
     * @throws Exception
     */
    public  JSONObject zjinv_fespcl_issuerXML(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        fsJson.put("data", data);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("fpHm", invoiceNum);
        data.put("fpDm", invoiceCode);
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        JSONArray items = new JSONArray();
        data.put("items", items);
        data.put("ssdq",json.getString("LocationOfInvoice"));
        data.put("airType",json.getString("SpecialInvoiceType")); // 特殊发票类型
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("bz", json.getString("Remarks")); //
        data.put("skr", json.getString("Payee")); //
        data.put("fhr", json.getString("Reviewer")); //
        data.put("kpr",json.getString("Issuer"));
        data.put("filepath", temppath);

        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("spbm", arr.getString("TaxClassificationCode"));
                item.put("xmmc", arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("ggxh",arr.getString("Specification"));
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("xmsl",arr.getBigDecimal("QuantityOfGoodsOrServices"));
                item.put("xmdj",arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje",arr.getBigDecimal("AmountExcludingTax"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100);
                item.put("se",arr.getBigDecimal("TaxAmount"));
                items.add(item);
            }
        }
        return fsJson;

    }

    /**
     * xml默认解析格式 普通发票
     * @param json
     * @return
     * @throws Exception
     */
    public JSONObject otherXml(JSONObject json, String fplx,String temppath) throws Exception {
        JSONObject fsJson = new JSONObject();
        fsJson.put("billType","invoice");
        String invoiceNum =  "".equals(json.getString("NumberOfInvoice")) ? "" : json.getString("NumberOfInvoice");
        String invoiceCode =  "".equals(json.getString("CodeOfInvoice")) ? "" : json.getString("CodeOfInvoice");
        fsJson.put("invoiceNum", invoiceNum); // 发票号码
        fsJson.put("invoiceCode", invoiceCode); // 发票代码
        JSONObject data = new JSONObject();
        JSONArray items = new JSONArray();
        fsJson.put("data", data);
        data.put("items", items);
        data.put("xsfMc", json.getString("NameOfSeller"));
        data.put("xsfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfSeller"));
        data.put("xsfDzdh",json.getString("AddressPhoneNumberOfSeller")); // 销方地址电话
        data.put("fpHm", json.getString("NumberOfInvoice"));
        data.put("fpDm", json.getString("CodeOfInvoice"));
        data.put("invoiceNum", invoiceNum); // 发票号码
        data.put("invoiceCode", invoiceCode); // 发票代码
        data.put("kprq", json.getString("DateOfIssue").replace("-", ""));
        data.put("fplx", fplx);
        data.put("jym", json.get("IdentifyingCode"));
        data.put("jshj", json.getBigDecimal("TaxIncludedAmountInFigures"));
        data.put("gmfMc", json.getString("NameOfPurchaser"));
        data.put("gmfNsrsbh", json.getString("TaxpayerIdentificationNumberUnifiedSocialCreditCodeOfPurchaser"));
        data.put("gmfYhzh", json.getString("DepositBankAndAccountNumberOfPurchaser"));
        data.put("hjje", json.getBigDecimal("TotalAmountExcludingTax"));
        data.put("hjse", json.getBigDecimal("TotalTaxAmount"));
        data.put("skr", json.getString("Payee"));
        data.put("kpr", json.getString("Issuer"));
        data.put("filepath", temppath);
        data.put("xsfYhzh", json.getString("AddressPhoneNumberOfSeller"));
        data.put("yfpDm",json.getString("CodeOfOriginalInvoice"));
        data.put("yfpHm",json.getString("NumberOfOriginalInvoice"));
        JSONObject invoiceExt = new JSONObject();
        invoiceExt.put("thirdVerifyStatus",0);
        data.put("invoiceExt", invoiceExt);
        Object object = json.get("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
        if (object instanceof com.alibaba.fastjson.JSONArray) {
            JSONArray jsonarr = json.getJSONArray("InformationOfTaxableGoodsOrServicesDetailItemsTuple");
            for (int i = 0; i < jsonarr.size(); i++) {
                JSONObject arr = jsonarr.getJSONObject(i);
                JSONObject item = new JSONObject();
                item.put("dw",arr.getString("UnitOfGoodsOrServices"));
                item.put("ggxh",arr.get("Specification"));
                item.put("se", arr.getBigDecimal("TaxAmount"));
                item.put("sl", Double.parseDouble(arr.get("TaxRate").toString().replace("%", "")) / 100);
                item.put("xmdj", arr.getBigDecimal("UnitPriceOfGoodsOrServices"));
                item.put("xmje", arr.getBigDecimal("AmountExcludingTax"));
                item.put("xmmc", arr.getString("NameOfTaxableGoodsOrServices"));
                item.put("xmsl", arr.getBigDecimal("QuantityOfGoodsOrServices"));
                items.add(item);
            }
        }
        return fsJson;
    }

}
